

import guireportgenerator.WebReportGenerator;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pool.bean.Transaction_Bean;
import pool.bean.Transaction_details_Bean;
import control.RequestParameter;
import dbmanager.DBManager;
import dbmanager.GetIdName;

/**
 * Servlet implementation class for Servlet: TransactionUpdateServlet
 *
 */
 public class TransactionUpdateServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public TransactionUpdateServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		System.out.println("I'm Report update servlet");
		Enumeration<Object> enum1 = request.getParameterNames();
		HttpSession session = request.getSession(true);
	        
	    Hashtable<Object,Object> old_hash = new Hashtable<Object,Object>();
	    Hashtable<Object,Object> new_hash = new Hashtable<Object,Object>();
	    
	    String username = (String)session.getAttribute("username");
	    String password = (String)session.getAttribute("password");
	    String poolrequest = (String)session.getAttribute("request");
	    
		PrintWriter out = response.getWriter();       
	    
	    GetIdName get = new GetIdName();
	    String master = null;
	    boolean sendresp = false;
	    
		try
		{
			sendresp = false;
        	while (enum1.hasMoreElements())
			{
				try 
				{
					String param = (String) enum1.nextElement();
					String id = request.getParameter(param);
					sendresp = false;
					
					if (param.equalsIgnoreCase("id")) 
					{
						//response.sendRedirect("EditTransactionJsp.jsp");
						ResultSet rsTran = DBManager.getSelect("select * from transaction where id="+param);
						Hashtable<Object, Object> hash = new Hashtable<Object, Object>();
						hash.put("id", id);
						
						while(rsTran.next())
						{
							hash.put("buyer", rsTran.getString("buyer"));
							hash.put("seller", rsTran.getString("seller"));
							hash.put("delivery_date", rsTran.getString("delivery_date"));
						}
						
						WebReportGenerator web = new WebReportGenerator(id,hash,"");
						out.write(web.getData());
						out.flush();
					}
					else 
					{
						// Let's update transaction first
						Hashtable<Object,Object> trans_hash = (Hashtable<Object,Object>)session.getAttribute("transaction");
						
						id = (String)trans_hash.get("id");
						String transaction_type = (String)session.getAttribute("transaction_type");
			        	String new_buyer = request.getParameter("buyer");
			        	String new_seller = request.getParameter("seller"); 
			        	String new_delivery_date = request.getParameter("delivery_date");
			        	
			        	String old_buyer = (String)trans_hash.get("Buyer");
			        	String old_seller = (String)trans_hash.get("Seller"); 
			        	String old_delivery_date = (String)trans_hash.get("Delivery_date");
			        	
			        	Transaction_Bean prop = new Transaction_Bean();
			    		//prop.setDBManager(db);
			    		
			        	RequestParameter req = new RequestParameter();
			    		req.setUsername(username);
			    		req.setMessage("System Message");
			    		req.setPassword(password);
			    		req.setRequest("transaction");
			    		req.setProcess("mod");
			        	prop.setReqParam(req);
			        	
			        	old_hash = new Hashtable<Object, Object>();
			        	old_hash.put("id", id);
			        	old_hash.put("Transaction_type", transaction_type);
			        	old_hash.put("buyer", old_buyer);
			        	old_hash.put("seller", old_seller);
			        	old_hash.put("delivery_date", old_delivery_date);
			    		
			    		//prop.setOldTransactionHashUpdate(old_hash);
			        	prop.setOldHashtable(old_hash);
			    		
			    		new_hash = new Hashtable<Object, Object>();
			    		new_hash.put("id", trans_hash.get("id"));
			    		new_hash.put("Transaction_type", transaction_type);
			    		new_hash.put("buyer", new_buyer);
			    		new_hash.put("seller", new_seller);
			    		new_hash.put("delivery_date", new_delivery_date);
			    		
			    		//prop.setNewTransactionHash(new_hash);
			    		prop.setNewHashtable(new_hash);
			    		
			    		//prop.updateTransaction();
			    		prop.update();
			    					    					    		   		
			    		// let's update transaction details now
			    		
			    		Transaction_details_Bean prop_details = new Transaction_details_Bean();
			    		
			    		req.setRequest("transaction_details");
			    		prop_details.setReqParam(req);
			    		//prop_details.setDBManager(db);
			    		
			    		Hashtable<Object, Object> item_qty = new Hashtable<Object, Object>();
			    		
			    		String old_itemlist = session.getAttribute("transaction_details").toString();
			        	String new_itemlist = request.getParameter("itemlist");
			        	ResultSet rs = null;
			        	
			        	// set old itemlist
			        	
			        	String [] old_items = old_itemlist.split(String.valueOf("\t"));
			        	
			        	for(int i=0;i<old_items.length;i++)
			        	{
			        		System.out.println(old_items[i]);
			        		String [] temp = old_items[i].split(String.valueOf(":"));
			        		
			        		try
			        		{
				        		String item = temp[0];
				        		String qty = temp[1];
					            
					            System.out.println(item);
					            System.out.println(qty);
					            
					            item_qty = new Hashtable<Object, Object>();
					            
					            rs = DBManager.getSelect("select PV from property_details where mid ='"+get.getId(item)+"' and pid='"+get.getId("itemcode")+"'");
					            rs.next();
					            
					            item_qty.put("item",rs.getString("PV"));
						        item_qty.put("qty",qty);
						        System.out.println(item_qty.toString());
						        
						        prop_details.setOldPropertyHash(item_qty);
			        		}
			        		catch(Exception e)
			        		{
			        			e.printStackTrace();
			        		}
			        	}        	
			        	
			        	String [] new_items = new_itemlist.split(String.valueOf("\t"));			        	
			        	
			        	for(int i=0;i<new_items.length;i++) 
			        	{
			        		System.out.println(new_items[i]);
			        		String [] temp = new_items[i].split(String.valueOf(":"));
			        		
			        		try
			        		{
				        		String item = temp[0];
				        		String qty = temp[1];
					            
					            System.out.println(item);
					            System.out.println(qty);
					            
					            item_qty = new Hashtable<Object, Object>();
					            
					            rs = DBManager.getSelect("select PV from property_details where mid ='"+get.getId(item)+"' and pid='"+get.getId("itemcode")+"'");
					            rs.next();					            
					            item_qty.put("item",rs.getString("PV"));
						        item_qty.put("qty",qty);
						        System.out.println(item_qty.toString());
						        
						        prop_details.setNewPropertyHash(item_qty);
			        		}
			        		catch(Exception e)
			        		{
			        			e.printStackTrace();
			        		}
						}
			        	
			    		//prop_details.updateTransaction_details();
			        	sendresp = true;
			        	
			        	if (sendresp) {
							response.sendRedirect("ViewServlet?objectname="+ get.getId(id));
						}						
					}
				}
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				finally
				{
					out = null;
					get = null;
				}
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}   	  	    
}